package exception.test;

public class TtyCatchFinally {
    public static void main(String[] args) {
//        System.out.println(test1());
        System.out.println(test2());
    }

    public static int test1() {
        int a = 10;
        try {
            System.out.println("执行了try");
            int b = a / 0;
            return 10;
        } catch (Exception e) {
            System.out.println("执行了catch");
            return 20;
        } finally {
            System.out.println("执行了finally");
            return 30;
        }
    }

    public static int test2() {
        int a = 10;
        try {
            System.out.println("执行了try");
            int b = a / 0;
            //先return 但是不会立即返回 而是保存一个临时副本
            //再看finally
            //有 则替换临时副本来返回
            //无 则返回临时副本
            return a;
        } catch (Exception e) {
            System.out.println("执行了catch");
            return 20;
        } finally {
            System.out.println("执行了finally");
            a += 20;
//            return a;
        }
    }
}
